 /* qDump.js (qJSLib)
 
    @Summary    : Debug Dump Frame Source for qJSLib
    @Author     : Timothy C. Quinn
    @License    : http://www.opensource.org/licenses/mit-license.php
    
    @Dependencies:
     - qCore.js (qJSLib)
     - qDump.html (qJSLib)
   
    @Notes:
     - Sources needs review and complete re-write to remove any undesirable sources
   
    @Todo:
     - 
*/  
bDebug=true;
    function procDebug(){
        try{
            var 
                o,a,aSpecs=[],s,aRet,sN,sA,sT,sMsg,sQcName,oQC

            if(!(sQcName=oURI.getParam("qcName"))){
                debugErr("Parameter qcName is required!")
            }
            if (!(o=_w.opener)) {
                alert("No Window Opener to Debug!"); window.close() 
            }else if (!(o=o[sQcName])) {
                debugErr("No QClient Defined: Name passed: "+sQcName); window.close() 
            } else if (!(a=o.debugVars)) {
                debugErr("No debug Vars Defined"); window.close() 
            } else {
                dw("<table cellspacing=0 cellpadding=2 border=1 width=100% bgcolor=#FFFFFF bordercolor=white>\n")
                dw("<tr><td class=Head style='font-size: 17px'><u><i>iDash Debugger</i></u></td><tr>")
              // Message
                if(isStr(sMsg=_w.opener._DebugDumpMessage)){
                    sMsg=sMsg.replace(/"JAVASCRIPT EXCEPTION\n"/,"").replace(/\n/g,"<br>").replace(/ /g,"&nbsp;")
                    dw("<tr><td class=SubHead style='padding-left: 10px; text-align: left'><div>~1</div><br></td></tr>\n".format(sMsg))
                }
                dw("</tr></table><br>\n")
              // Data Dump
                dw("<table cellspacing=0 cellpadding=2 border=1 width=100% bgcolor=#FFFFFF bordercolor=white>\n")          
                dw("<tr><td class=Head colspan=3><u><i>Environment Dump</i></u></td><tr>")
                dw("<tr><td class=Head>Description</td><td class=Head>Variable</td><td class=Head>Value</td></tr>\n")
                for (var i=0;i<a.length;i++){
                    if(!a[i]){
                        dw("<tr><td class=SLbl nowrap>~1</td><td class=SLblBld nowrap>~2:&nbsp;</td><td class=SAtt>~3&nbsp;</td></tr>\n".format("Bad Item","Bad Item",""))
                    }else{sN=a[i][0];sA=a[i][1]; sT=a[i][2]                    
                        if(sA){
                            if(sN.indexOf(" ")>-1 && sN.indexOf("[")==-1){debugErr("Variable Name cannot have spaces: "+sN)}
                            aRet=debugParseVar(sN,sT)
                            if(aRet[1]){aSpecs.p([sA,aRet[1]])}
                            dw("<tr><td class=SLbl nowrap>~1</td><td class=SLblBld nowrap>~2:&nbsp;</td><td class=SAtt>~3&nbsp;</td></tr>\n".format(sA,sN+(sT=="nv"?" (nv)":""),aRet[0]))
                        }else{
                            dw("<tr><td colspan=3 class=HeadCL>"+a[i]+"&nbsp;</td></tr>\n")
                        }
                    }
                }
                dw("</tr></table>")
                if(aSpecs.length>0){
                    dw("<br><div class=T3B align='center' width=100%><u><i>Table / Record Specifications</i></u></div><br>")
                    dw("<table cellspacing=0 cellpadding=2 border=1 width=100% bgcolor=#FFFFFF bordercolor=white>\n")
                    dw("<tr><td class=Head>Variable</td><td class=Head>Specification</td></tr>\n")
                    for (var i=0;i<aSpecs.length;i++){dw("<tr><td class=SLblBld>~1</td><td class=SAtt>~2</td></tr>".format(aSpecs[i][0],aSpecs[i][1]))}
                    dw("</tr></table>")
                }
                dw("<div align='center' style='font: bold 13px arial,times'><br>~ END OF iDASH DEBUG ~</div><br>")
            }
        }catch(e){debugErr(e)}
        function debugParseVar(sVar,sType){
            var 
                s,a=[[],[]],aFn=[],a2=[[],[]],aJSRSpec,sR="",aN,nv,aKeys,
                wo=window.opener
            if(isUndef(eval("s=wo."+sVar))){return ["(Undefined)"]}

            
            if (isArray(s)) {
//if(sVar === "_rwf.sects")
//	alert(
//		"!! qDump.js - isArray !!"
//	   +"\n sVar        = "+sVar
//	   +"\n sType       = "+sType
//	   +"\n s           = "+s$(s).abbrev(20)
//	   +"\n isArray(s)  = "+isArray(s)
//	   +"\n isObject(s) = "+isObject(s)
//	   +"\n s.isJSTable = "+s.isJSTable
//	   +"\n s.isJSRecord = "+s.isJSRecord
//	   +"\n s[q$.MK_isDb1Table] = "+s[q$.MK_isDb1Table]
//	   +"\n s[q$.MK_isDb1Rec] = "+s[q$.MK_isDb1Rec]
//	)
                if (s.isJSTable) {
//alert(
//	"\n s           = "+s$(s).abbrev(20)
//   +"\n isArray(s)  = "+isArray(s)
//   +"\n isObject(s) = "+isObject(s)
//   +"\n s.isJSTable = "+s.isJSTable
//   +"\n s.isJSRecord = "+s.isJSRecord
//)
                    if(s.length==0)
                        a=["~None~"]
                    else{
                        a=[s.fields.concat("Attributes")]
                        for (var i=0;i<s.length; i++) {
                            aa=[]; 
                            for (var ii=0;ii<s.fields.length; ii++) { aa.p(s[i][s.fields[ii]]) };
                            wo._NV=(isDef(s[i].att) && qObjListProps(s[i].att).length>0) ? s[i].att.get() : []
                            aa.p(wo._NV.length>0 ? debugParseVar("_NV","nv") : "-")
                            a.p(aa)
                        }
                    }
                } else if (s[q$.MK_isDb1Table]) {
//alert(
//    "!! qDump.js - isDb1Table !!"
//   +"\n sVar        = "+sVar
//   +"\n sType       = "+sType
//   +"\n s           = "+s$(s).abbrev(20)
//   +"\n isArray(s)  = "+isArray(s)
//   +"\n isObject(s) = "+isObject(s)
//   +"\n s.isJSTable = "+s.isJSTable
//   +"\n s.isJSRecord = "+s.isJSRecord
//   +"\n s[q$.MK_isDb1Table] = "+s[q$.MK_isDb1Table]
//   +"\n s[q$.MK_isDb1Rec] = "+s[q$.MK_isDb1Rec]
//)
                    if(s.length==0)
                        a=["~None~"]
                    else{
                    	aKeys = s[q$.MK_fieldKeys]
                        a=[aKeys]
                        for (var i=0;i<s.length; i++) {
                            aa=[]; 
                            for (var ii=0;ii<aKeys.length; ii++) { aa.p(s[i][aKeys[ii]]) };
                            a.p(aa)
                        }
                    }
                } else if (sType=="nv")
                    a = buildNV(s)
                else
                    a=s   
                sR+=buildTable(a)
            } else if (isObject(s)) {
//if(sVar === "_rwf.rngObj")
//	alert(
//	    "!! qDump.js - isObject !!"
//	   +"\n sVar        = "+sVar
//	   +"\n sType       = "+sType
//	   +"\n s           = "+s$(s).abbrev(20)
//	   +"\n isArray(s)  = "+isArray(s)
//	   +"\n isObject(s) = "+isObject(s)
//	   +"\n s.isJSTable = "+s.isJSTable
//	   +"\n s.isJSRecord = "+s.isJSRecord
//	   +"\n s[q$.MK_isDb1Table] = "+s[q$.MK_isDb1Table]
//	   +"\n s[q$.MK_isDb1Rec] = "+s[q$.MK_isDb1Rec]
//	)
	            if (s.isJSRecord) {
                    a=[s.fields.concat("Attributes")]
                    aa=[]
                    for(var ii=0;ii<s.fields.length;ii++){aa.p(s[s.fields[ii]])}
                  
                    wo._NV=(isDef(s.att) && qObjListProps(s.att).length>0) ? s.att.get() : []
                    aa.p(wo._NV.length>0 ? debugParseVar("_NV","nv") : "-")
                    a.p(aa)
                } else if (s[q$.MK_isDb1Rec]) {
//alert(
//    "!! qDump.js - isDb1Rec !!"
//   +"\n sVar        = "+sVar
//   +"\n sType       = "+sType
//   +"\n s           = "+s$(s).abbrev(20)
//   +"\n isArray(s)  = "+isArray(s)
//   +"\n isObject(s) = "+isObject(s)
//   +"\n s.isJSTable = "+s.isJSTable
//   +"\n s.isJSRecord = "+s.isJSRecord
//   +"\n s[q$.MK_isDb1Table] = "+s[q$.MK_isDb1Table]
//   +"\n s[q$.MK_isDb1Rec] = "+s[q$.MK_isDb1Rec]
//)
                    aKeys = s[q$.MK_fieldKeys]
                    a=[aKeys]
                    aa=[]; 
                    for (var ii=0;ii<aKeys.length; ii++) { aa.p(s[i][aKeys[ii]]) };
                    a.p(aa)
                } else {
                	if(sType ==="dumpFull"){
                		a=["Obj Dump (full)", qObjDump(s).cleanForHtml()]
                	} else if(sType ==="dumpProps"){
                		a=["Obj Dump (props)", q$.dumpJson(s).cleanForHtml()]
                	} else {
                    	for(var n in s){if(isFunc(s[n])){aFn.p(n)}else{a[0].p(n);a[1].p(s[n])}}
                   	}
                }
                sR+=buildTable(a)
                if(aFn.length>0){sR+="<br>Methods: "+aFn.join(", ")}
            } else {
                sR=s
            }
            return [sR,(s.isJSRecord||s.isJSTable)?buildSpec(s):null]
            
            function buildTable(aD){
                var iT,iM=0,td="<td class=SAtt", td2="&nbsp;</td>",s=""
                for (var i=0; i<a.length; i++) {
                    iT=(a[i] instanceof Array)?a[i].length:1
                    iM=iT>iM?iT:iM
                }
                s += "<table border=1 bordercolor=white>"
                for (var i=0; i<aD.length; i++) {
                    s+="<tr>"
                    if (isArray(aD[i])) {
                        if (aD[i].length==iM) {
                            for(var ii=0;ii<aD[i].length;ii++){s+=td+">"+buildTableCellVal(aD[i][ii])+td2}
                        } else {
                            for (var ii=0; ii<aD[i].length; ii++) {
                                s+=ii<aD[i].length-1?td + ">"+buildTableCellVal(aD[i][ii])+td2:td+" colspan="+(iM-ii)+">"+buildTableCellVal(aD[i][ii])+td2
                            }
                        }
                    } else {
                        s+=td+" colspan="+iM+">"+aD[i]+td2
                    }
                    s+="</tr>"
                }
                s+="</table>"
                return s
            }
            function buildTableCellVal(o){
            	if(isStr(o))
            		return o.cleanForHtml()
            	else if(isArray(o))
            		return buildTable(o)
            	else if(isJNull(o))
            		return "{JNull}"
            	else if(isJson(o))
            		return q$.dump(o, true).cleanForHtml()
            	else
            		return q$.dump(o, true).cleanForHtml()
            }
            function buildNV(o){
                var nv, aR=[],aN
                if(isStr(o)) { nv=o.split(sNVD) }else if(isArray(o)){ nv=s }else{ return ["Invalid Type!"] }
                if(!isNVArray(nv))
                    return ["Not NV Array!"]
                else {
                    nv=nv.toJson(); aN=qObjListProps(nv); 
                    if(aN.length==0){ return ["(Empty)"] }
                    aR.p(["Name","Value"]); for(var i=0;i<aN.length;i++) { aR.p( [aN[i], nv[aN[i]]] ) }
                }
                return aR
            }
            function buildSpec(){
                var aF=["fields","alias","dTypes","split"]
                sR+="</td><td class=SAtt valign=top><br>Field Spec:<br>"
                a=[["#"]];for (var i=0;i<s.fields.length;i++){a[0].p(i+1)}
                for(var i=0;i<aF.length;i++){
                    aa=[aF[i]+":"]
                    for (var ii=0;ii<s.fields.length;ii++){try{aa.p(s[aF[i]][ii])}catch(e){aa.p(s[aF[i]])}}
                    if(aF[i]=="alias"){for(var ii=0;ii<s.fields.length;ii++){if(a[i][ii+1]==aa[ii+1]){aa[ii+1]="-"}}}
                    a.p(aa)
                }
                return buildTable(a)
            }
        }
    }